{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 导入 TensorFlow\n",
    "import tensorflow as tf\n",
    "# 即显模式\n",
    "tf.enable_eager_execution()\n",
    "\n",
    "# 生成数据\n",
    "n_example = 100\n",
    "X = tf.random_normal([n_example])\n",
    "# 称重误差\n",
    "noise = tf.random_uniform([n_example], -0.5, 0.5)\n",
    "Y = X * 2 + noise\n",
    "\n",
    "# 训练集\n",
    "train_x = X[:80]\n",
    "train_y = Y[:80]\n",
    "\n",
    "# 测试集\n",
    "test_x = X[80:]\n",
    "test_y = Y[80:]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<matplotlib.legend.Legend at 0x7fbeb4059b00>"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAXYAAAD8CAYAAABjAo9vAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADl0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uIDIuMi4zLCBodHRwOi8vbWF0cGxvdGxpYi5vcmcvIxREBQAAIABJREFUeJzt3Xt8VOW5L/DfQxLIiEi8BIXEBOxRUESIpm4u7S4XLdCmIaC2tcjBXXv4+LG0SlsqKEIjeECw6m7Vsw+7F229uyspoBa5HlvYqFCC3JVigASViwaLDGFInvPHzIRZs9aaS2bNzJqZ3/fzyYfMur4M4ck7z3re9xVVBRERZY9O6W4AERE5i4GdiCjLMLATEWUZBnYioizDwE5ElGUY2ImIsgwDOxFRlnEssItInohsEZHlTl2TiIji52SP/W4Auxy8HhERdUC+ExcRkVIA3wTwEICfRDv+oosu0t69eztxayKinLF58+ajqloc7ThHAjuAxwH8HEA3uwNEZAqA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GBHZVvSHSfhGZDKAKwCgNJMPCNAII7ZmUAjiUqvbFeI1DgT8Pi8gS+D9WOxLYHWhf0t6/SG0TkU9EpKeqfhT4OH7Y5hrB926fiKwDUAF/rjkZYnkvgsc0ikg+gO5IzUf8qG1T1WMhL/8T/udSbpLU/6uJUNXPQ75/XUSeEpGLVNUNc8gYZHwqRkTGALgXQLWqnrQ57F0Al4tIHxHpDP8DraRXT8RKRLqKSLfg9/A/ELZ8Mp8m6Xr/lgKYHPh+MgDTpwsROV9EugS+vwjAMAA7k9imWN6L0HbfDGCNTYcj5W0Ly1dXA9iVgnbFYymA/xmojhkM4HgwHZduInJJ8FmJiFwPf/w8FvmsNEn309tEvwDshT8nVx/4ClYj9ALweshx3wDwPvw9uftT2L7x8PdCWgB8AmBFePvgr2LYGvja4bb2pev9gz8vvRrAB4E/LwhsrwTwm8D3QwFsC7x32wDckYJ2md4LAA/C37kAgEIArwR+Nt8BcFkK/z2jtW1+4GdsK4C1APqlqm2B+78A4CMAvsDP3R0A7gRwZ2C/AHgy0P5tiFBJloa2TQ157zYCGJrK9y6eL448JSLKMhmfiiEiIiMGdiKiLMPATkSUZRjYiYiyDAM7EVGWYWAnIsoyDOxERFmGgZ2IKMv8f/UVAahZOTkOAAAAAElFTkSuQmCC\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "# 显示训练集\n",
    "import matplotlib.pyplot as plt\n",
    "plt.plot(train_x, train_y - noise[:80], color='green')\n",
    "plt.scatter(train_x, train_y)\n",
    "plt.legend(['Rule','Data'])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 模型"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "import tensorflow.contrib.eager as tfe\n",
    "# 定义模型\n",
    "class Model(object):\n",
    "    def __init__(self):\n",
    "        # 参数\n",
    "        # 初始化：随便给 参数 W 和 b 两个值\n",
    "        self.W = tfe.Variable(1.)\n",
    "        self.b = tfe.Variable(1.)\n",
    "    \n",
    "    def __call__(self, x):\n",
    "        # 正向传递\n",
    "        # 如何利用 参数 W 和 b 运算\n",
    "        y = self.W * x + self.b\n",
    "        return y\n",
    "# 实例模型\n",
    "model = Model()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 训练"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Epoch: 000 | Loss: 0.461 | W: 1.563 | b: 0.391\n",
      "Epoch: 001 | Loss: 0.149 | W: 1.806 | b: 0.150\n",
      "Epoch: 002 | Loss: 0.094 | W: 1.913 | b: 0.056\n",
      "Epoch: 003 | Loss: 0.084 | W: 1.960 | b: 0.021\n",
      "Epoch: 004 | Loss: 0.082 | W: 1.981 | b: 0.007\n",
      "Epoch: 005 | Loss: 0.081 | W: 1.991 | b: 0.003\n",
      "Epoch: 006 | Loss: 0.081 | W: 1.995 | b: 0.001\n",
      "Epoch: 007 | Loss: 0.081 | W: 1.998 | b: 0.000\n",
      "Epoch: 008 | Loss: 0.081 | W: 1.999 | b: 0.000\n",
      "Epoch: 009 | Loss: 0.081 | W: 1.999 | b: 0.000\n",
      "Final Test Loss: 0.10177076\n",
      "[1.9995652 3.9986885]\n"
     ]
    },
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAXYAAAD8CAYAAABjAo9vAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADl0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uIDIuMi4zLCBodHRwOi8vbWF0cGxvdGxpYi5vcmcvIxREBQAAIABJREFUeJzt3Xl8VdW5//HPYwjNYYwSlCYBSR1wRMBgUej9XaUKVbRcf+JI1XolVUrVqlTRXgdKlRqrVqvXUr0OLQ5IFY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",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "# 误差\n",
    "def loss(prediction, label):\n",
    "    loss = tf.reduce_mean(tf.square(prediction - label))\n",
    "    return loss\n",
    "\n",
    "# 反向传递\n",
    "def train(model, x, y, learning_rate, batch_size, epoch):\n",
    "    # 次数\n",
    "    for e in range(epoch):\n",
    "        # 批量\n",
    "        for b in range(0,len(x.numpy()),batch_size):\n",
    "            # 梯度\n",
    "            with tf.GradientTape() as tape:\n",
    "                loss_value = loss(model(x[b:b+batch_size]), y[b:b+batch_size])\n",
    "                dW, db = tape.gradient(loss_value, [model.W, model.b])\n",
    "                #print(dW, db)\n",
    "            # 更新参数\n",
    "            model.W.assign_sub(dW * learning_rate)\n",
    "            model.b.assign_sub(db * learning_rate)\n",
    "        # 显示\n",
    "        print(\"Epoch: %03d | Loss: %.3f | W: %.3f | b: %.3f\" %(e, loss(model(x), y), model.W.numpy(), model.b.numpy()))\n",
    "\n",
    "# 训练\n",
    "train(model, train_x, train_y, learning_rate = 0.01, batch_size = 2, epoch = 10)\n",
    "\n",
    "# 评估\n",
    "test_p = model(test_x)\n",
    "print(\"Final Test Loss: %s\" %loss(test_p, test_y).numpy())\n",
    "\n",
    "# 可视化\n",
    "plt.plot(test_x,test_y-noise[80:],color='red')\n",
    "plt.scatter(test_x,test_y)\n",
    "plt.scatter(test_x,test_p)\n",
    "plt.legend(['rule','real','predicted'])\n",
    "\n",
    "# 预测\n",
    "test_p = model([1,2])\n",
    "print(test_p.numpy())"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
